package com.smforj.ssm.frame.core.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/***
 * 获取spring context util
 * 通过该对象来获取你想要的Bean
 * 
 * 
 * @author Aaron Gao
 * @2018 2018年4月26日
 * 
 *
 */
public class SpringContextHolder implements ApplicationContextAware {

	 // Spring应用上下文环境  
    private static ApplicationContext applicationContext;  
  
    /** 
     * 实现ApplicationContextAware接口的回调方法。设置上下文环境 
     *  
     * @param applicationContext 
     */  
    public void setApplicationContext(ApplicationContext applicationContext) {
    	//System.out.println("setApplicationContext#####################"+applicationContext.getApplicationName());
        SpringContextHolder.applicationContext = applicationContext;  
    }  
  
    /** 
     * @return ApplicationContext 
     */  
    public static ApplicationContext getApplicationContext() {  
        return applicationContext;  
    }  
  
    /** 
     * 获取对象 
     *  
     * @param name 
     * @return Object
     * @throws BeansException 
     */  
    public static Object getBean(String name) throws BeansException {   
    	//System.out.println("getBean#####################"+applicationContext.getApplicationName());
    	return applicationContext.getBean(name); 
    }  

}
